package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.EsLendFormProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.EsLendFormMapper;
import com.ruoyi.system.domain.EsLendForm;
import com.ruoyi.system.service.IEsLendFormService;

/**
 * 商品借出单(es_lend_form)Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-06
 */
@Service
public class EsLendFormServiceImpl implements IEsLendFormService
{
    @Autowired
    private EsLendFormMapper esLendFormMapper;

    /**
     * 查询商品借出单(es_lend_form)
     *
     * @param id 商品借出单(es_lend_form)主键
     * @return 商品借出单(es_lend_form)
     */
    @Override
    public EsLendForm selectEsLendFormById(Long id)
    {
        return esLendFormMapper.selectEsLendFormById(id);
    }

    /**
     * 查询商品借出单(es_lend_form)列表
     *
     * @param esLendForm 商品借出单(es_lend_form)
     * @return 商品借出单(es_lend_form)
     */
    @Override
    public List<EsLendForm> selectEsLendFormList(EsLendForm esLendForm)
    {
        return esLendFormMapper.selectEsLendFormList(esLendForm);
    }

    /**
     * 新增商品借出单(es_lend_form)
     *
     * @param esLendForm 商品借出单(es_lend_form)
     * @return 结果
     */
    @Override
    public int insertEsLendForm(EsLendForm esLendForm) {
        esLendForm.setCreateTime(DateUtils.getNowDate());
        esLendForm.setStatus(0); // New status
        int rows = esLendFormMapper.insertEsLendForm(esLendForm);

        if (rows > 0 && esLendForm.getProductList() != null) {
            Long lendId = esLendForm.getId();
            for (EsLendFormProduct product : esLendForm.getProductList()) {
                product.setLendId(lendId);
                esLendFormMapper.insertEsLendFormProduct(product);
            }
        }

        return rows;
    }

    /**
     * 修改商品借出单(es_lend_form)
     *
     * @param esLendForm 商品借出单(es_lend_form)
     * @return 结果
     */
    @Override
    public int updateEsLendForm(EsLendForm esLendForm) {
        int rows = esLendFormMapper.updateEsLendForm(esLendForm);

        if (rows > 0) {
            // Delete existing products
            esLendFormMapper.deleteEsLendFormProductByLendId(esLendForm.getId());

            // Add new products
            if (esLendForm.getProductList() != null) {
                for (EsLendFormProduct product : esLendForm.getProductList()) {
                    product.setLendId(esLendForm.getId());
                    esLendFormMapper.insertEsLendFormProduct(product);
                }
            }
        }

        return rows;
    }

    /**
     * 批量删除商品借出单(es_lend_form)
     *
     * @param ids 需要删除的商品借出单(es_lend_form)主键
     * @return 结果
     */
    @Override
    public int deleteEsLendFormByIds(Long[] ids)
    {
        return esLendFormMapper.deleteEsLendFormByIds(ids);
    }

    /**
     * 删除商品借出单(es_lend_form)信息
     *
     * @param id 商品借出单(es_lend_form)主键
     * @return 结果
     */
    @Override
    public int deleteEsLendFormById(Long id)
    {
        return esLendFormMapper.deleteEsLendFormById(id);
    }

    @Override
    public int confirmEsLendForm(Long id) {
        EsLendForm esLendForm = esLendFormMapper.selectEsLendFormById(id);
        if (esLendForm != null && esLendForm.getStatus() == 0) {
            esLendForm.setStatus(1);
            esLendForm.setConfirmTime(new Date());
            return esLendFormMapper.updateEsLendForm(esLendForm);
        }
        return 0;
    }

    @Override
    public int returnEsLendForm(Long id) {
        EsLendForm esLendForm = esLendFormMapper.selectEsLendFormById(id);
        if (esLendForm != null && esLendForm.getStatus() == 1) {
            esLendForm.setStatus(2);
            esLendForm.setReturnTime(new Date());
            return esLendFormMapper.updateEsLendForm(esLendForm);
        }
        return 0;
    }

    @Override
    public List<EsLendFormProduct> selectAvailableProducts(EsLendFormProduct product) {
        return esLendFormMapper.selectAvailableProducts(product);
    }


}
